home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / customicon / custicon.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  8.8 KB  |  359 lines

  1. /*
  2.     File:        CustIcon.c
  3.  
  4.     Contains:    This snippet shows how to use custom document icons in an application.  
  5.                 The correct procedure for doing this is to add the Icon family 
  6.                 to the document and set bit 10 of the finder info.
  7.     
  8.                 An elegant way of adding the icon family is to use one of the routines 
  9.                 described in the Icon Utilities chapter of Inside Macintosh More Toolbox 
  10.                 Essentials:   ForEachIconDo().  You can define  an action proc that is called 
  11.                 each time for each icon an an icon suite.
  12.  
  13.     Written by: NICKT    
  14.  
  15.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  16.  
  17.                 You may incorporate this Apple sample source code into your program(s) without
  18.                 restriction. This Apple sample source code has been provided "AS IS" and the
  19.                 responsibility for its operation is yours. You are not permitted to redistribute
  20.                 this Apple sample source code as "Apple sample source code" after having made
  21.                 changes. If you're going to re-distribute the source, we require that you make
  22.                 it clear in the source that the code was descended from Apple sample source
  23.                 code, but that you've made changes.
  24.  
  25.     Change History (most recent first):
  26.                 7/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  27.                 8/96:                         CustIcon.h added as prefix file for 68K and PPC MC 
  28.                                             projects; old routine names changed; void *myDataPtr
  29.                                              removed from definition of routine MyIconAction; FSSpec
  30.                                              theFSSpec and short theRef removed from HandleMenuCommand;
  31.                                              Rect screenRect and GrafPtr oldPort removed from
  32.                                             MainEventLoop; OSErr retCode, long gestResponse, EventRecord
  33.                                             Event and short count removed from InitToolbox; long
  34.                                             lByteCnt removed from HandleKeyPress
  35.                 
  36.  
  37. */
  38. #define    WWIDTH        470
  39. #define    WHEIGHT        330
  40.  
  41. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  42. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  43.  
  44. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  45. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  46.  
  47. #include <Processes.h>
  48. #include <DiskInit.h>
  49. #include <Resources.h>
  50. #include <Menus.h>
  51. #include <Devices.h>
  52. #include <StandardFile.h>
  53. #include <Dialogs.h>
  54. #include <Fonts.h>
  55. #include <Icons.h>
  56. #include <Finder.h>
  57. #include <Script.h>
  58.  
  59. //------------------------------------------------------------------------------
  60. // enumerated types for the menus
  61.  
  62. enum {
  63.     mApple = 128,
  64.     mFile
  65. } ;
  66.  
  67. enum {
  68.     iAbout = 1
  69. } ;
  70. enum {
  71.     iNew = 1,
  72.     iIcon,
  73.     iUnused1,
  74.     iQuit = 4
  75. } ;
  76.  
  77. //------------------------------------------------------------------------------
  78.  
  79. static Boolean pQuitFlag = false ;
  80.  
  81. //------------------------------------------------------------------------------
  82. // function prototypes
  83.  
  84. void InitToolbox( void ) ;
  85. void MainEventLoop( void ) ;
  86. void HandleKeyPress(EventRecord *event) ; 
  87. void HandleMenuCommand(long menuResult) ;
  88. void AdjustMenus( void ) ;
  89. pascal OSErr    MyIconAction( ResType    theType,
  90.                               Handle    *theIcon);
  91. OSErr TouchDir(short vRefNum, long dirID) ;
  92.  
  93. //-----------------------------------------------------------------------------
  94.  
  95.  
  96. void HandleMenuCommand(long menuResult)
  97. {
  98.     short        menuID;
  99.     short        menuItem;
  100.     Str255        daName;
  101.     DialogPtr    theDialog ; 
  102.     short        itemHit ;
  103.     SFTypeList    myTypes = { '????' } ;
  104.     Handle        myIconSuite ;
  105.     OSErr        theErr ;
  106.     
  107.     short        savedResFile ;
  108.     short        theResFile ;
  109.     
  110.     FInfo        myFndrInfo ;
  111.         
  112.     StandardFileReply    theSFReply ;
  113.  
  114.     menuID = HiWrd(menuResult);
  115.     menuItem = LoWrd(menuResult);
  116.     switch ( menuID ) {
  117.         case mApple:
  118.             switch ( menuItem ) {
  119.                 case iAbout:
  120.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  121.                     SetDialogDefaultItem(theDialog, 1) ;
  122.  
  123.                     do {
  124.                         ModalDialog ( nil, &itemHit );
  125.                     } while( itemHit != ok ) ;
  126.                     DisposeDialog ( theDialog );
  127.                     break;
  128.                     
  129.                 default:
  130.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  131.                     (void) OpenDeskAcc(daName);
  132.                     break;
  133.             }
  134.             break;
  135.         case mFile:
  136.             switch ( menuItem ) {
  137.                 case iNew:
  138.                     // create a file
  139.                     // put up a standard file put dialog to get the name
  140.                     StandardPutFile("\pCreate File", "\pUntitled", &theSFReply) ;
  141.                     if( theSFReply.sfGood ) {
  142.                         FSpCreateResFile( &theSFReply.sfFile, '????', 'TEXT', smSystemScript) ;
  143.                         theErr = ResError();
  144.                     }
  145.                     break ;
  146.                     
  147.                 case iIcon:
  148.                     // add icon family to file
  149.                     
  150.                     // put up std get file dlog
  151.                     StandardGetFile( nil, -1, myTypes, &theSFReply ) ;
  152.                     if( theSFReply.sfGood ) {
  153.                         
  154.                         // save the current resource file
  155.                         savedResFile = CurResFile();
  156.                         
  157.                         // Load the icon suite we want to set up
  158.                         theErr = GetIconSuite( &myIconSuite, 129, svAllAvailableData )  ;
  159.                         
  160.                         // get the finder info for the file
  161.                         theErr = FSpGetFInfo( &theSFReply.sfFile, &myFndrInfo ) ;
  162.                         
  163.                         // set bit 10 (has custom icon) and unset the inited flag
  164.                         // kHasBeenInited is 0x0100 so the mask will be 0xFEFF:
  165.                         
  166.                         myFndrInfo.fdFlags = 0xFEFF & (myFndrInfo.fdFlags | kHasCustomIcon ) ;
  167.                         
  168.                         // write it back
  169.                         theErr = FSpSetFInfo( &theSFReply.sfFile, &myFndrInfo ) ;
  170.                         
  171.                         // just check it already has a resource fork
  172.                         FSpCreateResFile( &theSFReply.sfFile, '????', 'TEXT', smSystemScript) ;
  173.                         theErr = ResError();
  174.                         
  175.                         // open the resfile the user picked
  176.                         theResFile = FSpOpenResFile ( &theSFReply.sfFile, fsWrPerm);
  177.                         theErr = ResError() ;
  178.                     
  179.                         UseResFile ( theResFile );
  180.                         theErr = ResError() ;
  181.                         
  182.                         theErr = ForEachIconDo( myIconSuite, svAllAvailableData, NewIconActionProc(MyIconAction), nil ) ;
  183.                         CloseResFile( theResFile );
  184.                         
  185.                         theErr = ResError() ;
  186.                         
  187.                         UseResFile ( savedResFile );
  188.                         
  189.                     }
  190.                     
  191.  
  192.                     break ;
  193.                 case iQuit:
  194.                 
  195.                     pQuitFlag = true;
  196.                     break;
  197.             }
  198.             break;
  199.             
  200.     }
  201.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  202. }
  203.  
  204. //---------------------------------------------------------------------------
  205. // write out the icon to the currently set res file
  206.  
  207. pascal OSErr    MyIconAction( ResType    theType,
  208.                               Handle    *theIcon)
  209.                               //void        *myDataPtr )
  210. {
  211.  
  212.     OSErr    theErr = noErr ;
  213.     long    lByteCnt = GetHandleSize( *theIcon );
  214.  
  215.     // theIcon is already a resource.  If we try to do an AddResource, it will fail
  216.     // with error -194, to prevent this, call detach resource.  You will need
  217.     // to reload the suite if you want to use it after this.
  218.     
  219.     DetachResource( *theIcon ) ;
  220.     if(( theErr = ResError()) != noErr )
  221.         return theErr ;    
  222.         
  223.     // write out each resource we are passed by ForEachIconDo
  224.     AddResource ( *theIcon, theType, kCustomIconResource, "\pCustom Finder Icon" );
  225.  
  226.     // check we are OK
  227.     theErr = ResError() ;
  228.  
  229.     return theErr ;
  230.     
  231. }
  232.  
  233. //---------------------------------------------------------------------------
  234.  
  235. void MainEventLoop()
  236. {
  237.     EventRecord     event;
  238.     WindowPtr       window;
  239.     short           thePart;
  240.     Point            aPoint = {100, 100};
  241.  
  242.     while( !pQuitFlag )
  243.     {
  244.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  245.         {
  246.             AdjustMenus() ;
  247.  
  248.             switch (event.what) {
  249.                 case mouseDown:
  250.                 
  251.                     thePart = FindWindow( event.where, &window );
  252.                     
  253.                     switch( thePart ) {
  254.                         case inMenuBar: 
  255.                             HandleMenuCommand(MenuSelect(event.where));
  256.                             break;
  257.                         
  258.                         case inDrag:
  259.                             break ;
  260.                     
  261.                         case inContent:
  262.                             break ;
  263.                     
  264.                         case inGoAway:
  265.                             break ;
  266.                             
  267.                         default:
  268.                             break ;
  269.                     }
  270.                     break ;
  271.                             
  272.                         
  273.                 case updateEvt:
  274.                     break ;
  275.                     
  276.                 case keyDown:
  277.                 case autoKey:
  278.                     HandleKeyPress(&event);
  279.                     break;
  280.                     
  281.                 case diskEvt:
  282.                     if ( HiWrd(event.message) != noErr ) 
  283.                         (void) DIBadMount(aPoint, event.message);
  284.                     break;
  285.                     
  286.                 case osEvt:
  287.                 case activateEvt:
  288.                     break;
  289.  
  290.  
  291.             }
  292.         }
  293.     }
  294. }
  295.  
  296. //------------------------------------------------------------------------------
  297.  
  298. void HandleKeyPress(EventRecord *event)
  299. {
  300.     char    key;
  301.  
  302.     key = event->message & charCodeMask;
  303.     
  304.     // just check to see if we want to quit...
  305.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  306.         HandleMenuCommand(MenuKey(key));
  307.     } 
  308. }
  309.  
  310. //------------------------------------------------------------------------------
  311.  
  312. void AdjustMenus( void ) 
  313. {
  314.     // ha - we don't got no menus
  315. }
  316.  
  317. //------------------------------------------------------------------------------
  318.  
  319. void main()
  320. {
  321.     InitToolbox() ;
  322.     
  323.     MainEventLoop();
  324. }
  325.  
  326. //------------------------------------------------------------------------------
  327.  
  328. void InitToolbox()
  329. {
  330.     Handle        menuBar = nil;
  331.  
  332.     InitGraf((Ptr) &qd.thePort);
  333.     InitFonts();
  334.     InitWindows();
  335.     InitMenus();
  336.     TEInit();
  337.     InitDialogs(0L);
  338.     InitCursor();
  339.  
  340.     // initialize application globals
  341.     
  342.     pQuitFlag = false;
  343.     
  344.     
  345.     menuBar = GetNewMBar(128);                // Read menus into menu bar, MBAR res id is 128
  346.     
  347.     if ( menuBar == nil )
  348.          ExitToShell();                        // if we dont have it then quit - your app 
  349.                                              // needs a dialog here
  350.  
  351.     SetMenuBar(menuBar);                    // Install menus
  352.     DisposeHandle(menuBar);
  353.     
  354.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  355.  
  356.     DrawMenuBar();
  357. }
  358.  
  359.